/* rqporef.p  - REQUISITIONS MAINTAIN REQ/PO CROSSREFERENCE RECORD      */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*F0PN*/ /*V8:ConvertMode=NoConvert                                     */
/* REVISION: 8.5     LAST MODIFIED: 02/13/97    BY: B. Gates *J1Q2*     */
/* REVISION: 9.1     LAST MODIFIED: 08/12/00    BY: *N0KP* myb          */
/************************************************************************/


         {mfdeclre.i}


         define input parameter p_first_call as logical no-undo.
     define input parameter p_req_site like pod_site no-undo.
         define input parameter p_req_nbr like rqpo_req_nbr no-undo.
         define input parameter p_req_line like rqpo_req_line no-undo.
         define input parameter p_po_nbr like rqpo_po_nbr no-undo.
         define input parameter p_po_line like rqpo_po_line no-undo.
         define input parameter p_qty_ord like pod_qty_ord no-undo.
         define input parameter p_pod_um like pod_um no-undo.
     define output parameter p_req_um like rqd_um no-undo.
         define output parameter p_ok as log no-undo.

     define variable old_db as character no-undo.
     define variable err-flag as integer no-undo.
     define variable prev_qty as decimal no-undo.
     define variable conversion_factor as decimal no-undo.

         if p_first_call then do:
        /*FIRST TIME IN, WE SWITCH DB'S IF WE HAVE TO THEN
        CALL OURSELVES AGAIN, WHICH WILL EXECUTE THE ELSE BLOCK*/
        find si_mstr where si_site = p_req_site no-lock.

        if si_db <> global_db then do:
           old_db = global_db.
           {gprun.i ""gpalias3.p"" "(si_db, output err-flag)"}
        end.

            {gprun.i ""rqporef.p""
        "(input no, input p_req_site, input p_req_nbr,
        input p_req_line, input p_po_nbr,
        input p_po_line, input p_qty_ord, input p_pod_um,
        output p_req_um, output p_ok)"}

            /*SWITCH BACK TO CENTRAL PO DB IF WE HAVE TO*/
        if old_db <> global_db then do:
           {gprun.i ""gpalias3.p"" "(old_db, output err-flag)"}
        end.
     end.
     else do:
        prev_qty = 0.

            find rqpo_ref where rqpo_req_nbr = p_req_nbr
            and rqpo_req_line = p_req_line
            and rqpo_po_nbr = p_po_nbr
            and rqpo_po_line = p_po_line
            exclusive-lock no-error.

        find rqd_det where rqd_nbr = p_req_nbr
        and rqd_line = p_req_line
        no-lock no-error.

        if available rqpo_ref and not available rqd_det then do:
           delete rqpo_ref.
           leave.
        end.

        p_req_um = rqd_um.

            if p_qty_ord = 0 then do:
               if available rqpo_ref then delete rqpo_ref.
           p_ok = true.
        end.
        else do:
               if not available rqpo_ref then do:
                  create rqpo_ref.

                  assign
                  rqpo_req_nbr = p_req_nbr
                  rqpo_req_line = p_req_line
                  rqpo_po_nbr = p_po_nbr
                  rqpo_po_line = p_po_line
                  .

          if recid(rqpo_ref) = -1 then.
               end.

           conversion_factor = 1.

           if p_pod_um <> rqd_um then do:
              {gprun.i ""gpumcnv.p""
          "(input p_pod_um, input rqd_um, input rqd_part,
          output conversion_factor)"}
           end.

               rqpo_qty_ord = p_qty_ord * conversion_factor.
           p_ok = rqpo_qty_ord <> ?.
            end.



        /*CHECK AND SET THE OPEN AND APRV STATUS INDICATORS*/

            find rqm_mstr where rqm_nbr = p_req_nbr no-lock.
        {gprun.i ""rqsetopn.p"" "(input rqm_nbr)"}
     end.
